using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TestDamageManager : MonoBehaviour
{
    public static TestDamageManager instance;

    private void Awake()
    {
        if (instance != null)
            Destroy(this);
        instance = this;
    }

    public void SubmitDamage(DamageInfo damageInfo)
    {
        BuffHandle creatorBuffHandle = damageInfo.creator?.GetComponent<BuffHandle>();
        BuffHandle targetBuffHandle = damageInfo.target?.GetComponent<BuffHandle>();

        if (creatorBuffHandle)
        {
            foreach (var buffInfo in creatorBuffHandle.buffList)
            {
                buffInfo.buffData.OnHit?.Apply(buffInfo, damageInfo);
            }
        }

        if (targetBuffHandle)
        {
            foreach (var buffInfo in targetBuffHandle.buffList)
            {
                buffInfo.buffData.OnBeHurt?.Apply(buffInfo, damageInfo);
            }

            var component = damageInfo.target.GetComponent<Character>();

            if (component)
            {
                if (component.IsCanBeKill(damageInfo))
                {
                    foreach (var buffInfo in targetBuffHandle.buffList)
                    {
                        buffInfo.buffData.OnBeKill?.Apply(buffInfo, damageInfo);
                    }

                    if (component.IsCanBeKill(damageInfo))
                    {
                        if (creatorBuffHandle)
                        {
                            foreach (var buffInfo in creatorBuffHandle.buffList)
                            {
                                buffInfo.buffData.OnKill?.Apply(buffInfo, damageInfo);
                            }
                        }
                    }
                }
            }
        }
    }

    public Text test_hp;
    public Text test_attack;
    public Character character;
    public BuffHandle buffHandle;
    
    private void Start()
    {
        buffHandle = character.gameObject.GetComponent<BuffHandle>();
        BdUtils.assert(buffHandle, $"character[{character.gameObject.name}]'s component[buffHandle] is null!");
    }
    
    BdTools.TimeIntervalUtil interval_timer = new BdTools.TimeIntervalUtil(0.5f, is_run_when_first_start: true);

    private void Update()
    {
        test_hp.text = $"hp: {character.property.hp}";
        test_attack.text = $"attack: {character.property.attack}";
        
        if (interval_timer.is_time_to_run())
        {
            foreach (BuffInfo buffInfo in buffHandle.buffList)
            {
                BdUtils.log(buffInfo.buffData.name, ", curStack:",buffInfo.curStack, "--- durationTimer:", BdUtils.round(buffInfo.durationTimer, 2), ", tickTimer:", buffInfo.tickTimer);
                // BdUtils.log(BdUtils.conv_obj_to_json(buffInfo.buffData));
            }
        }

    }

    public void OnClickAddAttackBuff()
    {
        string script_object_path = "ScriptObjects/buffs/BuffData__add_attack";
        var buffData = Resources.Load<BuffData>(script_object_path);
        if (buffData != null)
        {
            Debug.Log($"Buff Loaded: {buffData.name}, buff_name: {buffData.buffName}, desc: {buffData.description}");
            var buffInfo = new BuffInfo();
            buffInfo.buffData = buffData;
            buffInfo.curStack = 1;
            buffInfo.target = character.gameObject;
            // buffInfo.buffData.priority = 10;

            buffHandle.AddBuff(buffInfo);
            // buffInfo.creator = null;
        }
        else
        {
            Debug.LogError($"script_object_path[{script_object_path}] not found!");
        }
    }

    public void btn_add_boom_buff()
    {
        string script_object_path = "ScriptObjects/buffs/BuffData__spawn_boom";
        var buffData = Resources.Load<BuffData>(script_object_path);
        if (buffData != null)
        {
            Debug.Log($"Buff Loaded: {buffData.name}, buff_name: {buffData.buffName}, desc: {buffData.description}");
            var buffInfo = new BuffInfo();
            buffInfo.buffData = buffData;
            buffInfo.curStack = 1;
            buffInfo.target = character.gameObject;

            buffHandle.AddBuff(buffInfo);
        }
        else
        {
            Debug.LogError($"script_object_path[{script_object_path}] not found!");
        }
    }

    private void OnGUI()
    {
        if (GUILayout.Button("增加攻击力 BUFF"))
        {
            OnClickAddAttackBuff();
        }
        if (GUILayout.Button("生成炸弹 BUFF"))
        {
            btn_add_boom_buff();
        }
        if (GUILayout.Button("调用hit"))
        {
            character.btn_hit();
        }
    }
}
